<?php

/**
 * tutor actions.
 *
 * @package    TutorMe
 * @subpackage tutor
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class tutorActions extends sfActions
{
  /**
   * Executes index action
   *
   */
/*  public function executeIndex()
  {
    $this->tutors = TutorPeer::getTutorByFirstName('James');
  }

  public function executeSearchSubject()
  {
	if ($this->getRequest()->getMethod() != sfRequest::POST)
	{
		return sfView::SUCCESS; //Display the form
	}
	else
	{
            $subject = SubjectPeer::getSubjectByName($this->getRequestParameter('subject'));
        
            $this->tutors = $subject->getTutors(4);

            return 'Results';
	}
   }
*/

  public function executeSearch()
  {
	if ($this->getRequest()->getMethod() != sfRequest::POST)
	{
            $subjects = SubjectPeer::doSelect(new Criteria());
            $this->subjects = array();

            foreach($subjects as $subject)
            {
              $this->subjects[$subject->getName()] = $subject->getName();
            }

	      $this->serviceareas= ServiceAreaPeer::doSelect(new Criteria());
		$this->availabilities = AvailabilityPeer::doSelect(new Criteria());
		
		return sfView::SUCCESS; //Display the form
	}
	else
	{
            $subject = SubjectPeer::getSubjectByName($this->getRequestParameter('subject'));
        
            $this->tutors = $subject->getTutors(99, 
								$this->getRequestParameter('rates'),
								$this->getRequestParameter('servicearea'),
								$this->getRequestParameter('availability'));
		$this->serviceareaname = ServiceAreaPeer::retrieveByPK($this->getRequestParameter('servicearea'));
		$this->availabilityname = AvailabilityPeer::retrieveByPK($this->getRequestParameter('availability'));
		if(!($this->tutors))
		{
			$this->redirect('@notutor');
		}

            return 'Results';
	}    
  }

  public function handleErrorSearch()
  {
		$subjects = SubjectPeer::doSelect(new Criteria());
            $this->subjects = array();

            foreach($subjects as $subject)
            {
              $this->subjects[$subject->getName()] = $subject->getName();
            }

	      $this->serviceareas= ServiceAreaPeer::doSelect(new Criteria());
		$this->availabilities = AvailabilityPeer::doSelect(new Criteria());
		
		return sfView::SUCCESS; //Display the form
  }

  public function executeTutorcount()
  {
		$this->count = TutorPeer::doCount(new Criteria());
  }

  public function executeInfo()
  {
		sfConfig::set('sf_web_debug', false);

		$tutorid = $this->getRequestParameter('tutorid');
		$tutor = TutorPeer::retrieveByPK($tutorid);
		$info = $tutor->getAdditionalInfo();
            
            if(is_null($info))
		{
			return $this->renderText("<html><body><br />No Additional Info</body></html>");
		}
		else
		{
			return $this->renderText("<html><body><br />".$info."</body></html>");
		}
  }

  public function executeMail()
  {
	sfConfig::set('sf_web_debug', false);
	$this->setLayout(false);

	if ($this->getRequest()->getMethod() != sfRequest::POST)
	{
		$this->userx = sfGuardUserPeer::retrieveByUsername($this->getRequestParameter('email'));
		$tutor = $this->userx->getTutors();
		$this->tutor = $tutor[0];

		return sfView::SUCCESS; //Display the form
	}
	else
	{
		$email = $this->sendEmail('mail','sendTutorContact');

		return 'sent';
	}
  }

  public function handleErrorMail()
  {
	sfConfig::set('sf_web_debug', false);
	$this->setLayout(false);

	$this->userx = sfGuardUserPeer::retrieveByUsername($this->getRequestParameter('email'));
	$tutor = $this->userx->getTutors();
	$this->tutor = $tutor[0];

	return sfView::SUCCESS; //Display the form
  }
}
